
package uk.ac.cam.cl.oscar.sift;

import java.io.*;
import java.util.*;

/** To put keypoint lists into the database, and to retrive them they
	require serialisation and deserialisation. This class performs these
	and wraps the results up nicely
	*/
public class KeypointPacker {

	/* In preparedStatement, you can just call 'addObject' with the byte array
	   to assign data to the Blob. Sorted! 
		*/
	public static byte[] toByteArray (Vector<Keypoint> keypoints) 
	{
		byte[] buf = null;
		
		try
		{
			ByteArrayOutputStream b = new ByteArrayOutputStream(300);
			ObjectOutputStream out = new ObjectOutputStream(b);
			out.writeObject(keypoints);
			out.close();
			buf = b.toByteArray();
			b.close();
		}
		catch(IOException ex)
		{
			ex.printStackTrace();
		}
		
		return buf;
	}
	
	
	/* To retrieve the keypoint data back from the database. In ResultSet 
	   call getBinaryStream, and you have something to pass to this method 
	   */
	public static Vector<Keypoint> fromBinaryStream (InputStream stream)
	{
		Vector<Keypoint> list = null;
		
		try
		{
			ObjectInputStream in = new ObjectInputStream(stream);
			list = (Vector<Keypoint>) in.readObject();
			in.close();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		return list;
	}

}